package com.ihr360.oneclick.controller;

import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.oneclick.entity.StaffRecord;
import com.ihr360.oneclick.request.SearchRequest;
import com.ihr360.oneclick.request.StaffMessageRecordRequest;
import com.ihr360.oneclick.request.StaffRecordAllowRequest;
import com.ihr360.oneclick.request.query.StaffRecordQueryParam;
import com.ihr360.oneclick.service.SpreadsheetRowService;
import com.ihr360.oneclick.service.StaffRecordService;
import com.ihr360.oneclick.vo.PageData;
import com.ihr360.oneclick.vo.StaffMessageRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 员工名录相关接口
 *
 * @author tom
 * @date 17-12-25
 */
@RestController
@RequestMapping("/staffRecord")
@Api(value = "/staffRecord", description = "员工名录相关接口信息", produces = ("application/json;charset=UTF-8"))
public class StaffRecordController {

    @Autowired
    private StaffRecordService staffRecordService;
    @Autowired
    private SpreadsheetRowService spreadSheetRowService;

    @PostMapping(value = "/pageDate")
    @ApiOperation(value = "/pageDate", httpMethod = "POST", notes = "获取员工名录分页数据",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<PageData<StaffRecord>> getPageDate(@RequestBody SearchRequest<StaffRecordQueryParam> searchRequest, AuthUserInfo authUserInfo) {
        PageData<StaffRecord> pageData = staffRecordService.getPageData(authUserInfo.getUserId(), authUserInfo.getCompanyId(), searchRequest);
        return new ResultInfo.Builder<PageData<StaffRecord>>().success(pageData);
    }

    @GetMapping(value = "/staffRecord/{id}")
    @ApiOperation(value = "/staffRecord/{id}", httpMethod = "GET", notes = "获取某一个员工名录信息",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<StaffRecord> getStaffRecord(@PathVariable String id, AuthUserInfo authUserInfo) {
        StaffRecord staffRecord = staffRecordService.getStaffRecordById(authUserInfo.getCompanyId(), id);
        return new ResultInfo.Builder<StaffRecord>().success(staffRecord);
    }

    @PostMapping(value = "/updateAllow")
    @ApiOperation(value = "/updateAllow", httpMethod = "POST", notes = "更新员工命令查看权限",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo updateAllow(@RequestBody StaffRecordAllowRequest staffRecordAllowRequest, AuthUserInfo authUserInfo) {
        staffRecordService.updateAllow(authUserInfo.getCompanyId(),
                staffRecordAllowRequest.getId(), staffRecordAllowRequest.getAllow());
        return new ResultInfo.Builder<>().success();
    }

    @PostMapping(value = "/updateAllowBatch")
    @ApiOperation(value = "/updateAllowBatch", httpMethod = "POST", notes = "批量更新员工命令查看权限",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo updateAllowBatch(@RequestBody StaffRecordAllowRequest staffRecordAllowRequest, AuthUserInfo authUserInfo) {
        staffRecordService.updateAllowBatch(authUserInfo.getCompanyId(),
                staffRecordAllowRequest.getIdList(), staffRecordAllowRequest.getAllow());
        return new ResultInfo.Builder<>().success();
    }

    @PostMapping(value = "/messageRecord")
    @ApiOperation(value = "/messageRecord", httpMethod = "POST", notes = "查看消息记录",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<StaffMessageRecordVo> messageRecord(@RequestBody StaffMessageRecordRequest staffMessageRecordRequest,
                                                          AuthUserInfo authUserInfo) {
        StaffMessageRecordVo staffMessageRecordVo = spreadSheetRowService.getStaffRecordSpreadsheetRowPageData(
                authUserInfo.getCompanyId(), authUserInfo.getUserId(), staffMessageRecordRequest.getStaffRecordId(),
                staffMessageRecordRequest.getTermId(), staffMessageRecordRequest.getSearchRequest());
        return new ResultInfo.Builder<StaffMessageRecordVo>().success(staffMessageRecordVo);
    }

}
